package models;

import java.util.List;
import java.util.regex.Pattern;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;


import java.util.regex.Matcher;
import beans.PageBean;
import utils.StringUtils;

/**
 * 
 * @author Cheng
 * 2014/9/16
 */
@Entity
@Table(name="user")
public class User extends BasicModel{
    public static final int SuperAdmin = 1;
    public static final int Admin = 1;
    public static final int user = 0;
	//账号
	@Column(name = "account")
	public String account;
	//密码
	@Column(name = "password")
	public String password;
    //邮箱
    @Column(name = "email")
    public String email;

    //邮箱
    @Column(name = "weibo")
    public String weibo;
    //昵称
    @Column(name = "nickname")
    public String nickname;

    //个人地址
    @Column(name = "address")
    public String address;
    
    //从事领域
    @Column(name = "workArea")
    public String workArea;
    
    //座右铭
    @Column(name = "motto")
    public String motto;

    //性别,0男,1女
    @Column(name = "sex", columnDefinition = "int default 0", nullable = false)
    public int sex;

    //个人签名
    @Column(name = "sign")
    public String sign;

    //有邮件要收
    @Column(name = "hasEmail",columnDefinition = "int default 0")
    public int hasEmail;

    //个人说明
    @Column(name = "myExplain")
    public String myExplain;

    //手机号码
    @Column(name = "phone")
    public String phone;

    //权限,0普通用户,1系统管理员,2超级管理员
    @Column(name = "authority",columnDefinition = "int default 0", nullable = false)
    public int authority;

    //网站
    @Column(name = "website")
    public String website;
    
    //文章数量
    @Column(name = "articleNum",columnDefinition = "int default 0")
    public int articleNum;
    
    //头像
    @Column(name = "photoId")
    public String photoId;
    
    //粉丝数量
    @Column(name = "fansNum",columnDefinition = "int default 0")
    public int fansNum;
    
    //关注的数量
    @Column(name = "concernNum",columnDefinition = "int default 0")
    public int concernNum;

	public static List<User> findUserByQuery(String query,PageBean pageBean){
		return User.find("nickname like ? ", String.format("%%%s%%", query)).fetch(pageBean.getCurPage(),pageBean.getPerPage());
	}
	public static long CountUserByQuery(String query){
		return User.count("nickname like ? ", String.format("%%%s%%", query));
	}
	
	public static User findByUsername(String username){
		return User.find("account = ?", username).first();
	}
	
	public static String getAuthorNameById(String id)
	{
		User user = User.findById(id);
		if(user == null)
			return "";
		else
			return user.nickname;
	}
	
	public boolean isAllNotNull(){
		if(!StringUtils.isEmpty(this.password)&&(!StringUtils.isEmpty(this.account))){
			return true;
		}
		else{
		return false;
		}
	}

    public String getPhoto(){
        Photo photo = Photo.find("id= ?",this.photoId).first();
        if(photo!=null){
            return photo.getPhoto();
        }
        else{
            return "../public/images/tt.png";
        }
    }
	public static boolean matchTheEmail(String email)
	{
		Pattern pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
		Matcher matcher = pattern.matcher(email);
		return matcher.matches();
	}
	
	public static boolean matchThePhoneNO(String phone)
	{
		 Pattern pattern = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");     
	     Matcher matcher = pattern.matcher(phone);   
	     return matcher.matches();
	}
}
